package com.CityHunter.Activity;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.CityHunter.R;
import com.CityHunter.DataObject.Prey;
import com.CityHunter.DataObject.Schedule;
import com.CityHunter.OverLay.ShowPreyOverlay;
import com.CityHunter.OverLay.TouchOverLay;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class SearchAct extends MapActivity{

	private MapController mController;
	private LocationManager mLocationManager;
	private MapView mView;
	private GeoPoint destination;
	private Location mLocation;
	
	private int zoomLevel;
	
	private String strLocationProvider = "";
	
	private List<Overlay> overlays;
	private List<Prey> preys;
	
	private ShowPreyOverlay showPreyOverlay;
	private TouchOverLay scheduleOverlay;
	
	private static Schedule schedule;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.schedule_map);
		
		zoomLevel = 17;
		
		/*
		// Get lat and lng from bundle
		Bundle bundle = this.getIntent().getExtras();
		double[] lat = bundle.getDoubleArray("lat");
		double[] lng = bundle.getDoubleArray("lng");
		destination = new GeoPoint((int)(lat[0]*1E6) , (int)(lng[0]*1E6));
		preys = new ArrayList<Prey>();
		preys.add(new Prey("Friend from bundle",destination));
		*/
		
		// Define MapView and MapController
		mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		mView = (MapView)findViewById(R.id.mView);
		mController = mView.getController();
		mView.setSatellite(false);
		mView.setStreetView(true);
		mView.displayZoomControls(true);
		mController.setZoom(zoomLevel);
		mLocation = getLocationProvider();

		if (mLocation != null)
			mController.animateTo(getGeoByLocation(mLocation));
		
		overlays = mView.getOverlays();
		
		if (schedule == null)
			schedule = new Schedule();

		/*
		Drawable preyMarker;
		preyMarker = getResources().getDrawable(R.drawable.prey);
		showPreyOverlay = new ShowPreyOverlay(preyMarker,this,preys);
		overlays.add(showPreyOverlay);
		*/
		
		scheduleOverlay = new TouchOverLay(this,R.drawable.checkpoint,schedule);
		overlays.add(scheduleOverlay);
		//mController.animateTo(destination);
		Log.d("Debug","MapPage init.");
		
	}

	@Override
	public void onStop(){
		Log.d("Debug",schedule.toString());
		super.onStop();
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return true;
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
		// param 1 : group id , param 2:itemId, param 3:item order, param 4:item
		// name
		menu.add(0, 0, 0, R.string.lbl_scheduleDone);
		return super.onCreateOptionsMenu(menu);
	}
	
	// Handle Menu Item onClick Event
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
		case 0:
			Intent i = new Intent(SearchAct.this,ScheduleDetailPage.class);
			startActivity(i);
			break;
		}
		return true;
	}
	
	// Get Location Provider
	public Location getLocationProvider(){
		Location retLocation = null;
		try{
			Criteria mCriteria = new Criteria();
			mCriteria.setAccuracy(Criteria.ACCURACY_FINE);
			mCriteria.setAltitudeRequired(false);
			mCriteria.setBearingRequired(false);
			mCriteria.setCostAllowed(true);
			mCriteria.setPowerRequirement(Criteria.POWER_LOW);
			//strLocationProvider = lm.getBestProvider(mCriteria,false);
			strLocationProvider = "network";
			retLocation = mLocationManager.getLastKnownLocation(strLocationProvider);
			Log.d("Debug","Best Provider = "+ strLocationProvider);
			
			if (retLocation == null)
				Log.d("Debug","retLocation = null");
			else 
				Log.d("Debug","retLocation = "+ retLocation.toString());
		} catch (Exception e){
			Log.e("Error","Error occur here.");
			e.printStackTrace();
		}
		return retLocation;
	}

	private GeoPoint getGeoByLocation(Location location){
		GeoPoint gp = null;
		try {
			if (location != null){
				double geoLatitude = location.getLatitude()*1E6;
				double geoLongitude = location.getLongitude()*1E6;
				gp = new GeoPoint((int)geoLatitude,(int)geoLongitude);
			}
		} catch (Exception e){
			e.printStackTrace();
		}
		return gp;
	}
	
	private String GeoPointToString(GeoPoint gp){
		String strReturn = "";
		try {
			if (gp != null){
				double geoLatitude = (int)gp.getLatitudeE6()/1E6;
				double geoLongitude = (int)gp.getLongitudeE6()/1E6;
				strReturn = String.valueOf(geoLatitude) + "," + String.valueOf(geoLongitude);
			}
		} catch (Exception e){
			e.printStackTrace();
		}
		return strReturn;
	}
	
}
